** For "Who Holds Sovereign Debt and Why It Matters" by Fang, Hardy, and Lewis
clear all
set more off
cd "~/Dropbox/Sovereign Bond Project/Replication_Package_submission_RFS"

global varlist Total_Debt Domestic_Total Domestic_CentralBank Domestic_Bank Domestic_NonBank Foreign_Total Foreign_Official Foreign_Bank Foreign_NonBank
global cate_8_list Domestic_Bank Domestic_CentralBank Domestic_NonBank Foreign_Bank_Non_EA Foreign_Bank_EA Foreign_NonBank_Non_EA Foreign_NonBank_EA Foreign_Official 
global NB_list InsurPens OthFin HH NFC


************************************************************************
****                             Data                             ****
************************************************************************
// yearly
use "Data/Euro_Area_Data/ECB_Data.dta", clear // million in Euro
destring year, replace
destring quarter, replace
keep if quarter == 4 // keep Q4, to merge with yearly data
drop if year > 2018
tostring year, replace
merge n:1 year using "Data/Euro_Area_Data/Exchange_Rate.dta" // Euro exchange rate
drop _merge

destring year, replace

// divide by exchange_rate
foreach x in All Gov Banks Nonbanks HH InsurPens NFC OthFin {
    replace `x' = `x' / ex_rate
}
drop ex_rate quarter

rename country Country
rename year Year

save "Data/Euro_Area_Data/ECB_Data_USD.dta", replace // million in USD 

import excel "Data/Paper_Data/FLZ_Data.xlsx", firstrow clear
destring Year, replace
drop if mi(Total_Debt, Domestic_Total, Domestic_CentralBank, Domestic_Bank, Domestic_NonBank, Foreign_Total, Foreign_Official, Foreign_Bank, Foreign_NonBank)
do "Code/00_Country_Definition.do"

foreach x in $varlist GDP {
    replace `x' = `x' / 1000000 // million
}

merge 1:1 Country Year using "Data/Euro_Area_Data/ECB_Data_USD.dta"
keep if _merge == 3
drop _merge

save "Data/Euro_Area_Data/ECB_Combined_Data.dta", replace


************************************************************************
****                Yearly Data, 8-Category Investor                ****
************************************************************************
use "Data/Euro_Area_Data/ECB_Combined_Data.dta", clear
distinct Country // 43
tab Country

replace Foreign_NonBank = 0 if Foreign_NonBank < 0 
replace Domestic_NonBank = 0 if Domestic_NonBank < 0 

gen Foreign_Bank_Non_EA = Foreign_Bank - Banks
gen Foreign_Bank_EA = Banks
gen Foreign_NonBank_Non_EA = Foreign_NonBank - Nonbanks
gen Foreign_NonBank_EA = Nonbanks

encode Country, generate(cid)
xtset cid Year
sort cid Year

foreach x in Total_Debt $cate_8_list {
	gen d_`x' = d.`x'
	gen d_`x'_Tot = d_`x'/l.Total_Debt
}

// All Non-EA
foreach x in $cate_8_list {
	reghdfe d_`x'_Tot d_Total_Debt_Tot if EA!=1, absorb(cid Year) vce(cluster cid)
} 

************************************************************************
****              Quarterly Data, Nonbank Decomposition             ****
************************************************************************
// quarterly
use "Data/Euro_Area_Data/ECB_Data.dta", clear

rename country Country
rename year Year
rename quarter Quarter
destring Year, replace
destring Quarter, replace

gen Date = yq(Year, Quarter)
format Date %tq

do "Code/00_Country_Definition.do"

encode Country, generate(cid)
xtset cid Date
sort cid Date

foreach x in Nonbanks $NB_list {	
	gen d_`x' = d.`x'
	gen d_`x'_NB = d_`x'/l.Nonbanks
}

estimates clear
// All
foreach x in $NB_list  {
	reghdfe d_`x'_NB d_Nonbanks_NB if EA!=1, absorb(cid Date) vce(cluster cid)
} 

// AE
foreach x in $NB_list {
	reghdfe d_`x'_NB d_Nonbanks_NB if AE==1 & EA!=1, absorb(cid Date) vce(cluster cid)
} 

// EM
foreach x in $NB_list {
	reghdfe d_`x'_NB d_Nonbanks_NB if EM==1 & EA!=1, absorb(cid Date) vce(cluster cid)
}




 


